package com.dao.impl;

import com.bean.Books;
import com.dao.BooksDao;
import util.DateUtil;
import util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;


/**
 * Created by Administrator on 2018/11/23.
 */
public class BooksDaoImpl implements BooksDao {
    /**
     * 方法概述：账户登录数据处理
     *
     * @param books
     * @return
     */
    @Override
    public Books getPwd(Books books) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        Books b = null;
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "select id,account,pwd from user where account=?";
//预编译
            ps = conn.prepareStatement(sql);
//赋值
            ps.setString(1, books.getAccount());

            res = ps.executeQuery();

            if (res.next()) {
                b = new Books();
                b.setAccount(res.getString("account"));
                b.setPwd(res.getString("Pwd"));
                return b;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, res);
        }

        return null;
    }


    /**
     * 方法概述：  验证登录手机是否注册  并把过期时间和验证码存入数据库
     *
     * @param phone
     * @param code1
     * @return
     */

    @Override
    public boolean getPhone(String phone, String code1) {
        Connection conn = null;
        PreparedStatement ps = null;
        int a;
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "update user set veriCode =?,deadline=date_add(now(),interval 10 minute ) where phone=?";
//预编译
            ps = conn.prepareStatement(sql);
//赋值
            ps.setString(1, code1);
            ps.setString(2, phone);
            a = ps.executeUpdate();
            if (a > 0) {
                return true;
            } else {
                return false;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
        return false;
    }


    /**
     * 方法概述;验证手机登录
     *
     * @param phone
     * @return
     */
    @Override
    public HashMap<String, String> getCode(String phone) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet res = null;

        HashMap<String, String> hashMap = new HashMap<String, String>();
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "select id,phone,veriCode,deadline from user where phone=?";
//预编译
            ps = conn.prepareStatement(sql);
//赋值
            ps.setString(1, phone);
            res = ps.executeQuery();
            if (res.next()) {
                String getPhone = res.getString("phone");
                String code = res.getString("veriCode");
                String getDate = res.getString("deadline");
                        String date=date(getDate);
                hashMap.put("phone", getPhone);
                hashMap.put("code", code);
                hashMap.put("date", date);

                return hashMap;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, res);
        }
        return null;
    }


    /**
     * 方法概述：注册账号
     *
     * @param books
     * @return
     */
    @Override
    public boolean addAccount(Books books) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConnertion();
//            设置手动提交
//            conn.setAutoCommit(false);
            String sql = "insert into user (account,pwd,Createtime) value(?,?,date_add(now(),interval 0 minute))";
            ps = conn.prepareStatement(sql);

            ps.setString(1, books.getAccount());
            ps.setString(2, books.getPwd());

            if (!(ps.execute())) {
//                conn.commit();
//                System.out.println(a);
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }

        return false;
    }

    /**
     * 把注册时的验证码存入数据库
     *
     * @param phoneRig
     * @param code1
     */

    @Override
    public void addRig(String phoneRig, String code1) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConnertion();
//            设置手动提交
//            conn.setAutoCommit(false);
            String sql = "insert into rigcode (code,phone,deadline) value(?,?,date_add(now(),interval 10 minute))";
            ps = conn.prepareStatement(sql);
            ps.setString(1, code1);
            ps.setString(2, phoneRig);

            if (!(ps.execute())) {
//                System.out.println("成功");
//                conn.commit();
//                System.out.println(a);

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
    }

    /**
     * 方法概述： 删除验证码
     *
     * @param phone
     */
    @Override
    public void delCode(String phone) {
        Connection conn = null;
        PreparedStatement ps = null;
        int a;
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "update user set veriCode =? where phone=?";
//预编译
            ps = conn.prepareStatement(sql);
//赋值
            ps.setString(1, "0");
            ps.setString(2, phone);
            a = ps.executeUpdate();
//            if (a > 0) {
//                System.out.println("成功");
//            } else {
//                System.out.println("失败");
//            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
    }


    @Override
    public void delCodeRig(String phone) {
        Connection conn = null;
        PreparedStatement ps = null;
        int a;
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "delete from rigcode where phone=?";
//预编译
            ps = conn.prepareStatement(sql);
//赋值

            ps.setString(1, phone);
            a = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
    }

    /**
     * 验证账号是否存在
     *
     * @param account
     * @return
     */
    @Override
    public boolean getAccountRig(String account) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "select account from user where account=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, account);
            rs = ps.executeQuery();
            if (rs.next()) {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, rs);
        }

        return true;
    }

    /**
     * 方法概述:查询注册是输入的电话号码是否已经注册
     *
     * @param phoneRig
     * @return
     */
    @Override
    public boolean getPhoneRig(String phoneRig) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "select phone from user where phone=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, phoneRig);
            rs = ps.executeQuery();

            if (rs.next()) {
//                System.out.println(rs.getString("phone"));
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, rs);
        }

        return true;
    }


    @Override
    public HashMap<String, String> getCodeRig(String phoneRig, String codeRig) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet res = null;

        HashMap<String, String> map = new HashMap<String, String>();
        try {
            conn = JdbcUtil.getConnertion();
            String sql = "select phone,code,deadline from rigcode where phone=?";
//预编译
            ps = conn.prepareStatement(sql);
//赋值
            ps.setString(1, phoneRig);
            res = ps.executeQuery();
            if (res.next()) {
                String phone = res.getString("phone");
                String code = res.getString("code");
                String a = res.getString("deadline");
                String date = date(a);
                map.put("phone", phone);
                map.put("code", code);
                map.put("date", date);
                return map;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, res);
        }
        return null;
    }
//        public static void main(String[] args) {
//        new BooksDaoImpl().addPhoneRig("13408211158");
//    }
    /**
     * 方法概述:电话号码注册成功后  添加到数据库
     * @param phoneRig
     */
    @Override
    public void addPhoneRig(String phoneRig) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JdbcUtil.getConnertion();

            String sql = "insert into user(phone,Createtime) value(?,date_add(now(),interval 0 minute))";
            ps = conn.prepareStatement(sql);

            ps.setString(1, phoneRig);

            if (!(ps.execute())) {

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(conn, ps, null);
        }
    }

    /**
     * 方法概述:转换时间格式
     *
     * @param date
     * @return
     */
    public String date(String date) {
        Date date2 = null;
        try {
            date2 = DateUtil.dateString(date, "yyyy-MM-dd HH:mm:ss.SSS");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String date1 = DateUtil.stringDate(date2, "yyyy-MM-dd HH:mm:ss.SSS");
        String date3 = DateUtil.TimeStamp(date1, "yyyy-MM-dd HH:mm:ss.SSS");
        return date3;
    }

}

